package io.github.frailty9.crserver.dto

import io.github.frailty9.crserver.model.Resource
import java.time.LocalDate
import java.time.LocalTime

/**
 * API data json:
{
"name": "test",
"description": "",
"imageUrl": "",
"maxBookingsPerCycle": 1,
"unitPerPhysical": 1,
"unitName": "test",
"visibleToColleges": "",
"visibleToMajors": "",
"visibleToGrades": "",
"visibleToClasses": "",
"repeatType": "WEEKLY",
"bookingOpenMode": "string",
"bookingOpenBeforeSeconds": 10,
"effectiveStart": "2021-10-10",
"effectiveEnd": "2021-10-10",
"allowBookingOnUsage": true,
"repeatRules": [
{
"startTime": "09:00:00",
"endTime": "12:00:00",
"capacityUnits": 10,
"daysOfWeek": "1,2,3,4,5",
}
"daysOfMonth": "",
]
}
 *
 */

data class ResourcePublishDto(
    val name: String,
    val description: String,
    val imageUrl: String,
    val maxBookingsPerCycle: Int,
    val unitPerPhysical: Int,
    val unitName: String,
    val visibleToColleges: String,
    val visibleToMajors: String,
    val visibleToGrades: String,
    val visibleToClasses: String,
    val repeatType: String,
    val bookingOpenMode: String,
    val bookingOpenBeforeSeconds: Int,
    val effectiveStart: LocalDate,
    val effectiveEnd: LocalDate,
    val allowBookingOnUsage: Int,
    // ==== 不原型入表的字段 ====
    val repeatRules: List<RITemplateDto>,
) {
    fun toResource(creatorId: Long) = toResource(null, creatorId)
    fun toResource(resourceId: Long? = null, creatorId: Long) = Resource(
        id = resourceId ?: 0L,
        name = name,
        description = description,
        imageUrl = imageUrl,
        creatorId = creatorId,
        maxBookingsPerCycle = maxBookingsPerCycle,
        unitPerPhysical = unitPerPhysical,
        unitName = unitName,
        visibleToColleges = visibleToColleges,
        visibleToMajors = visibleToMajors,
        visibleToGrades = visibleToGrades,
        visibleToClasses = visibleToClasses,
        repeatType = repeatType,
        bookingOpenMode = bookingOpenMode,
        bookingOpenBeforeSeconds = bookingOpenBeforeSeconds,
        allowBookingOnUsage = allowBookingOnUsage,
        effectiveStart = effectiveStart,
        effectiveEnd = effectiveEnd,
        status = 1,
    )
}

data class RITemplateDto(
    val startTime: LocalTime,
    val endTime: LocalTime,
    val capacityUnits: Int,
    val daysOfWeek: String,
    val daysOfMonth: String,
)

data class ResourceBookingParam(
    val instanceId: Long,
    val bookUnit: Int
)
